package com.hemh.utils;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * @author HeMingHui
 * @since 2020-10-18
 * @description 时间的Util类
 */
public class TimeUtils {

	/** 得到当前的localDateTime */
	public static LocalDateTime getLocalDateTimeNow() {
        return LocalDateTime.now();
    }

	/** String 按照一定的格式转换成LocalDateTime */
	public static LocalDateTime parseStringToDateTime(String time, String format) {
		DateTimeFormatter df = DateTimeFormatter.ofPattern(format);
		return LocalDateTime.parse(time, df);
	}	
	
	/** localDateTime 按照一定的格式转换成String */
	public static String localDateTimeFormat(LocalDateTime localDateTime, String pattern) {
        return localDateTime.format(DateTimeFormatter.ofPattern(pattern));
    }

	/**Date 转换为LocalDateTime，默认时区为东8区 */
	public static LocalDateTime dateConvertToLocalDateTime(Date date) {
	        return date.toInstant().atOffset(ZoneOffset.of("+8")).toLocalDateTime();
	}

	/** LocalDateTime 转换为 Date，默认时区为东8区 */
	public static Date localDateTimeConvertToDate(LocalDateTime localDateTime) {
        return Date.from(localDateTime.toInstant(ZoneOffset.of("+8")));
    }
	
	/** 计算两个LocalDateTime 之间的毫秒数 */
	public static Long minusToMillsLocalDateTime(LocalDateTime time1, LocalDateTime time2) {
        return Duration.between(time1, time2).toMillis();
    }

	/** 计算两个Date之间相差的天数 */
	public static Long minuxToDaysByDate(Date startDate, Date endDate) {
		return (endDate.getTime()-startDate.getTime()) / (24 * 60 * 60 * 1000);
	}
	
}
